/**
 * 
 */

package com.kugou.game.sdk.ui.fragment;

import com.kugou.framework.utils.NetWorkUtil;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseSDKWorkerFragment;
import com.kugou.game.sdk.core.SDKActions;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.AccountSecurityInfo;
import com.kugou.game.sdk.interfaces.onAccountSecurityInfoListener;
import com.kugou.game.sdk.ui.widget.LoadingView;
import com.kugou.game.sdk.utils.Rx;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 描述:设置支付密码
 *
 * @author jerryliu
 * @since 2015年8月20日 上午11:47:32
 */
public class SecurityPsdSetFragment extends BaseSDKWorkerFragment {
    protected static final int MSG_BACK_SET_PAY_PSD = 11;

    protected static final int MSG_UI_SET_PAY_PSD_SUCCESS = 12;

    protected static final int MSG_UI_SET_PAY_PSD_FAILE = 13;

    private Context context;

    private TextView tvAccount, tvLoginPsdInputErrorTip, tvPayPsdInputErrorTip;

    private EditText etPayPsd, etLoginPsd;

    private Button btnConfirm;

    private LoadingView loadingView;

    private View layoutContent;

    private User user;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        user = UserManager.getInstance().getCurrentUserInternal();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(Rx.layout.kg_fragment_security_psd_set, null);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private void initView(View view) {
        loadingView = (LoadingView) view.findViewById(Rx.id.kg_loadingView);
        loadingView.setText("正在操作，请稍候…");
        layoutContent = view.findViewById(Rx.id.kg_layout_bind_phone);
        tvAccount = (TextView) view.findViewById(Rx.id.kg_tv_username);
        tvAccount.setText(user.getAccountName());
        tvLoginPsdInputErrorTip = (TextView) view.findViewById(Rx.id.kg_input_error_tip_1);
        tvPayPsdInputErrorTip = (TextView) view.findViewById(Rx.id.kg_input_error_tip_2);

        etPayPsd = (EditText) view.findViewById(Rx.id.kg_et_phone_number);
        etLoginPsd = (EditText) view.findViewById(Rx.id.kg_et_psd);
        btnConfirm = (Button) view.findViewById(Rx.id.kg_btn_confirm);
        btnConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                hideSoftInput(context);
                if (!NetWorkUtil.isNetworkAvailable(context)) {
                    showToast("请检查您的网络连接");
                    return;
                }

                String loginPsd = etLoginPsd.getText().toString().trim();
                if (TextUtils.isEmpty(loginPsd)) {
                    showToast("请输入登录密码");
                    tvLoginPsdInputErrorTip.setText("请输入登录密码");
                    tvLoginPsdInputErrorTip.setVisibility(View.VISIBLE);
                    return;
                } else {
                    tvLoginPsdInputErrorTip.setVisibility(View.GONE);
                }

                // if (!loginPsd.equals(user.getPassword())) {
                // showToast("您输入的登录密码不正确");
                // tvLoginPsdInputErrorTip.setText("您输入的登录密码不正确");
                // tvLoginPsdInputErrorTip.setVisibility(View.VISIBLE);
                // return;
                // } else {
                // tvLoginPsdInputErrorTip.setVisibility(View.GONE);
                // }

                String payPsd = etPayPsd.getText().toString().trim();
                if (TextUtils.isEmpty(payPsd) || payPsd.length() != 6) {
                    showToast("请输入6位支付密码");
                    tvPayPsdInputErrorTip.setText("请输入6位支付密码");
                    tvPayPsdInputErrorTip.setVisibility(View.VISIBLE);
                    return;
                } else {
                    tvPayPsdInputErrorTip.setVisibility(View.GONE);
                }

                tvLoginPsdInputErrorTip.setVisibility(View.GONE);
                tvPayPsdInputErrorTip.setVisibility(View.GONE);

                loadingView.setVisibility(View.VISIBLE);
                layoutContent.setVisibility(View.GONE);
                sendEmptyBackgroundMessage(MSG_BACK_SET_PAY_PSD);
            }
        });
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        final Message message = new Message();
        String username = user.getAccountName();
        String password = etLoginPsd.getText().toString().trim();
        String paypassword = etPayPsd.getText().toString().trim();
        switch (msg.what) {
            case MSG_BACK_SET_PAY_PSD:
                UserManager.getInstance().setPayPsd(username, password, paypassword,
                        new onAccountSecurityInfoListener() {
                            @Override
                            public void onSuccess(AccountSecurityInfo info) {
                                message.what = MSG_UI_SET_PAY_PSD_SUCCESS;
                            }

                            @Override
                            public void onFaile(String errorMsg) {
                                message.what = MSG_UI_SET_PAY_PSD_FAILE;
                                message.obj = errorMsg;
                            }
                        });
                break;
        }

        sendUiMessage(message);
    }

    @Override
    public void handleUiMessage(Message msg) {
        if (getActivity() == null || getActivity().isFinishing() || isDetached() || !isAdded()) {
            return;
        }

        loadingView.setVisibility(View.GONE);
        layoutContent.setVisibility(View.VISIBLE);
        switch (msg.what) {
            case MSG_UI_SET_PAY_PSD_SUCCESS:
                context.sendBroadcast(new Intent(SDKActions.ACTION_SET_PAY_PSD_SUCCESS));
                getActivity().finish();
                break;
            case MSG_UI_SET_PAY_PSD_FAILE:
                if (msg.obj != null) {
                    showToast(msg.obj.toString());
                }
                break;
        }
    }
}
